package com.neuedu.oasystem.view;

import java.util.List;
import java.util.Scanner;
import com.neuedu.oasystem.entity.Department;
import com.neuedu.oasystem.service.DepartmentService;
import com.neuedu.oasystem.service.impl.DepartmentServiceImpl;
/**
 * 部门界面
 * @author admin
 *
 */
public class DepartmentSystem {
	private Scanner scanner = null;
	Department department = null;
	private DepartmentService departmentService;

	public void init() {
		System.out.println("===================================");
		System.out.println("*==========欢迎使用部门管理系统======*");
		System.out.println("===================================");
		System.out.println("*============1.部门管理===========*");
		System.out.println("*============2.退出系统===========*");
		System.out.println("===================================");
	}

	public void work() {
		scanner = new Scanner(System.in);
		departmentService = new DepartmentServiceImpl();
		boolean running = true;
		while (running) {
			init();
			System.out.print("请选择：");
			String choose = scanner.next();
			switch (choose) {
			case "1":
				// 调用用管理模块
				System.out.println("调用部门管理模块");
				userManager();
				break;
			case "2":
				System.out.println("您选择了退出系统！");
				running = false;
				break;
			default:
				System.out.println("您的输入有误！");
				break;
			}
		}
	}

	private void userManager() {

		boolean running = true;
		while (running) {
			System.out.println("===================================");
			System.out.println("                                您选了权限管理模块  ");
			System.out.println("===================================");
			System.out.println("1.新增部门");
			System.out.println("2.修改部门");
			System.out.println("3.部门列表");
			System.out.println("4.删除部门");
			System.out.println("5.返回上一级");
			System.out.println("===================================");
			System.out.println("请选择：");
			String choose = scanner.next();
			switch (choose) {
			case "1":
				System.out.println("新增部门 ");
				deptAdd();
				break;
			case "2":
				System.out.println("修改部门 ");
				deptEdit();
				break;
			case "3":
				System.out.println("部门列表");
				deptList();
				break;
			case "4":
				System.out.println("删除部门 ");
				deptDelete();
				break;
			case "5":
				System.out.println("返回上一级");
				running = false;
				break;
			default:
				System.out.println("您的输入有误！");
				break;
			}

		}

	}

	private void deptAdd() {
		departmentService = new DepartmentServiceImpl();

		System.out.println("请添加部门名称");
		String deptName = scanner.next();
		System.out.println("请添加部门信息");
		String deptInfor = scanner.next();
		department = new Department(0, deptName, deptInfor);
		boolean rs = departmentService.add(department);
		if (rs) {
			System.out.println("添加成功！");
		}else {
			System.out.println("添加失败！");
		}
	}

	private void deptEdit() {
		deptList();
		departmentService = new DepartmentServiceImpl();
		System.out.println("请输入修改部门的Id");
		int deptId = scanner.nextInt();
		System.out.println("请修改部门名称");
		String deptName = scanner.next();
		System.out.println("请添加部门信息");
		String deptInfor = scanner.next();
		department = new Department(deptId, deptName, deptInfor);
		boolean rs = departmentService.edit(department);
		if (rs) {
			System.out.println("修改成功！");
		}else {
			System.out.println("修改失败！");
		}
	}

	private void deptList() {
		departmentService = new DepartmentServiceImpl();
		List<Department> list = departmentService.findAll();
		System.out.println("部门编号\t部门名称\t部门信息");
		for (Department department : list) {
			System.out.println(
					department.getDeptId() + "\t" + department.getDeptName() + "\t" + department.getDeptInfor());
		}
	}

	private void deptDelete() {
		departmentService = new DepartmentServiceImpl();
		deptList();
		System.out.println("请输入您要删除的部门编号");
		int deptId = scanner.nextInt();
		boolean rs = departmentService.deleteById(deptId);
		if (rs) {
			System.out.println("删除成功！");
		}else {
			System.out.println("删除失败！");
		}
		deptList();
	}

}
